var t = getApp();

Component({
    properties: {},
    data: {
        tempFilePath: "",
        hideModal: !0,
        hasDraw: !1,
        canvasName: "#handWriting",
        ctx: "",
        canvasWidth: 0,
        canvasHeight: 0,
        startPoint: {
            x: 0,
            y: 0
        },
        selectColor: "black",
        lineColor: "#1A1A1A",
        lineSize: 1,
        radius: 1.25
    },
    lifetimes: {
        ready: function() {
            var t = this, a = wx.createSelectorQuery().in(this);
            this.canvasCtx = wx.createCanvasContext("signature", this), this.canvasCtx.setFillStyle("#fff"), 
            this.canvasCtx.fillRect(0, 0, 500, 500), this.canvasCtx.draw(), this.canvasCtx.setLineCap("round"), 
            this.canvasCtx.setLineJoin("round"), this.canvasCtx.setStrokeStyle(this.data.selectColor), 
            a.select(".modal-canvas").boundingClientRect(function(a) {
                t.setData({
                    canvasWidth: a.width,
                    canvasHeight: a.height
                });
            }).exec();
        }
    },
    methods: {
        show: function() {
            this.setData({
                hideModal: !1
            });
        },
        closeModal: function() {
            this.clearCanvas(), this.setData({
                hideModal: !0
            }), this.triggerEvent("closeSignature");
        },
        scaleStart: function(t) {
            if ("touchstart" != t.type) return !1;
            var a = {
                x: t.touches[0].x,
                y: t.touches[0].y
            };
            this.drawCircle(a), this.setData({
                startPoint: a,
                hasDraw: !0
            });
        },
        mouseDown: function() {},
        scaleEnd: function(t) {
            this.setData({
                isStart: !0
            });
        },
        scaleMove: function(t) {
            if ("touchmove" != t.type) return !1;
            var a = this.data.startPoint, e = {
                x: t.touches[0].x,
                y: t.touches[0].y
            };
            this.drawLine(a, e), this.setData({
                startPoint: e
            });
        },
        drawCircle: function(t) {
            var a = this.canvasCtx;
            a.beginPath(), a.setFillStyle(this.data.lineColor), a.arc(t.x, t.y, this.data.radius, 0, 2 * Math.PI), 
            a.fill(), a.closePath(), a.draw(!0);
        },
        drawLine: function(t, a) {
            var e = this.canvasCtx;
            this.drawCircle(a), e.beginPath(), e.setStrokeStyle(this.data.lineColor), e.setLineWidth(2 * this.data.radius), 
            e.moveTo(t.x, t.y), e.lineTo(a.x, a.y), e.stroke(), e.closePath();
        },
        clearCanvas: function() {
            var t = this.canvasCtx;
            t.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight), t.fillStyle = "rgba(0, 0, 0, .1)", 
            t.draw(), this.setData({
                hasDraw: !1
            });
        },
        getTempFile: function() {
            return {
                tempFilePath: this.data.tempFilePath
            };
        },
        saveToImage: function() {
            var t = this.data, a = t.hasDraw, e = t.canvasHeight, i = t.canvasWidth, s = this;
            a ? wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: i,
                height: e,
                canvasId: "signature",
                success: function(t) {
                    t.tempFilePath && (s.setData({
                        tempFilePath: t.tempFilePath
                    }), s.upQrcodeImg(t.tempFilePath));
                },
                fail: function(t) {
                    console.log(t);
                }
            }, s) : wx.showToast({
                title: "您还没签名，请签名后再进行提交",
                icon: "none",
                mask: !0
            });
        },
        upQrcodeImg: function(a) {
            var e = null, i = this, s = t.getNetAddresss("upload.uploadPic");
            console.log(a), wx.uploadFile({
                url: s,
                filePath: a,
                name: "file",
                formData: null,
                success: function(t) {
                    var a = JSON.parse(t.data);
                    e = a.data.img, console.log("签名上传成功", e);
                },
                fail: function(t) {
                    console.log("签名上传失败", t);
                },
                complete: function(t) {
                    i.triggerEvent("confirm", e);
                }
            });
        }
    }
});